const mongoose = require('mongoose');
const  dotenv= require('dotenv');// 引入环境变量
dotenv.config({path:'./config.env'});
const app = require('./app.js');
const DB = process.env.DATABASE_LOCAL;

// 全局未处理的同步错误捕捉
process.on('uncaughtException',err=>{
    console.log(err.name,err.message);
    console.log('即将关闭');
    process.exit(1);
})
mongoose.connect(DB,{
    useNewUrlParser:true,
    useCreateIndex:true,
    useFindAndModify:false,
    useUnifiedTopology: true ,
}).then(()=> console.log('DB connection successful'))

const port = process.env.PORT|| 3000;
const server = app.listen(port, () => {
    console.log(`app runing on port ${port}`)
})
// 测试保存

// 全局未处理的异步错误捕捉
process.on('unhandledRejection',err=>{
    console.log(err.name,err.message);
    console.log('即将关闭');
    server.close(()=>{
        process.exit(1);
    })
    
})

